package cn.icyzx.dao;

import cn.icyzx.exception.GetUserException;
import cn.icyzx.exception.ModifyUserInfoException;
import cn.icyzx.exception.UserOperatorException;
import cn.icyzx.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.*;


import static cn.icyzx.util.StaticConstUtil.USER_ACTIVE_FALSE;


/**
 * Created by 郑凯 on 2016/7/18.
 */
@Repository("daoUser")
public class DaoUser {

    @Autowired
    UserMapper userMapper;

    @Autowired
    WorkMapper workMapper;


    public User login(String usernameOrEmail, String password) {
        User u = userMapper.login(usernameOrEmail, password);
        return u;
    }


    public boolean register(User user) {
        try {
            user.setUid(String.valueOf(new Date().getTime()));
            userMapper.insertUser(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }


    public List<String> collegeList() {
        try {
            return userMapper.collegeList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<String> majorList(String college) {
        try {
            return userMapper.majorList(college);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<String> classesList() {
        try {
            return userMapper.classesList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void updateBaseInfo(User user) throws ModifyUserInfoException {
        if (0 == userMapper.updateBaseInfo(user))
            throw new ModifyUserInfoException("修用户基本信息错误");
    }

    public void updateCollege(String uid, String college, String major, String classes) throws ModifyUserInfoException {
        if (0 == userMapper.updateCollege(uid, college, major, classes))
            throw new ModifyUserInfoException("修用户学院等错误");
    }

    public void updateUserAvatar(String avatarImg, String uid) throws UserOperatorException {
        if (userMapper.updateUserAvatar(avatarImg, uid) <= 0)
            throw new UserOperatorException("修改头像失败-未找到该用户");
    }

    public boolean insertWork(Work work) {
        try {
            userMapper.insertWorks(work);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    public boolean isExistName(String name) {

        try {
            User user = userMapper.getUserByUsername(name);
            return user == null ? false : true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void activeUser(String uid) throws UserOperatorException {
        if (userMapper.activeUser(uid) <= 0)
            throw new UserOperatorException("激活用户失败");
    }

    public User getUserByUsername(String name) {
        try {
            return userMapper.getUserByUsername(name);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public User getUserByUid(String uid) throws GetUserException {
        User user = userMapper.getUserByUid(uid);
        if (user == null)
            throw new GetUserException("找不到该用户");
        return user;
    }

    /**
     * checkEmail
     *
     * @param email 传入的email地址
     * @return boolean 存在返回true,不存在返回false
     */
    public boolean checkEmail(String email) {
        try {
            User user = userMapper.existEmail(email);
            return user == null ? false : true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * checkActive
     *
     * @param uid 传入的uid
     * @return boolean 激活返回true,否则返回false
     */
    public boolean checkActive(String uid) {

        User user = userMapper.isActive(uid);
        if (user == null)
            return false;
        if (USER_ACTIVE_FALSE.equals(user.getActive()))
            return false;
        else
            return true;
    }

    public User getUserByEmail(String email) throws GetUserException {
        User user = userMapper.getUserByEmail(email);
        if (user == null)
            throw new GetUserException("通过email获取User失败，不存在该email对应的用户");
        return user;
    }

    /**
     * 获取所有用户列表
     *
     * @return
     */
    public List<User> getUserList() {
        return userMapper.getUserList();
    }

    /**
     * 根据id删除指定用户
     *
     * @param uid
     */
    public void deleteUserByUid(String uid) {
        userMapper.deleteUserByUid(uid);
    }
}
